package com.wyg.epcommonbase.common;

import com.wyg.epcommonbase.common.exception.ExceptionEnum;
import com.wyg.epcommonbase.common.exception.MyException;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: WangYongGan
 * @Date: 2021/10/27 16:36
 * @Description:
 */
@Data
@AllArgsConstructor
public class ApiResult {

    private Integer code;
    private String msg;
    private Map<String, Object> data = new HashMap<String, Object>();

    private ApiResult() {
    }

    public static ApiResult ok() {
        return new ApiResult(0, "成功", new HashMap<>());
    }

    public static ApiResult error() {
        return new ApiResult(101, "请求失败", new HashMap<>());
    }

    public static ApiResult error(MyException exception) {
        return new ApiResult(exception.getCode(), exception.getMsg(), new HashMap<>());
    }

    public static ApiResult error(int code, String msg) {
        return new ApiResult(code, msg, new HashMap<>());
    }

    public static ApiResult error(ExceptionEnum exception) {
        return new ApiResult(exception.code, exception.msg, new HashMap<>());
    }

    public ApiResult data(String k, Object v) {
        data.put(k, v);
        return this;
    }

    public ApiResult message(String msg) {
        this.setMsg(msg);
        return this;
    }

    public ApiResult data(Map map) {
        this.data = map;
        return this;
    }

    public ApiResult data(Object data) {

        this.data.put("data", data);
        return this;
    }

}
